/************************************************************************
This file is part of Polar4linux
Copyright (C) 2010-2011 Eric jrdn2
Portions Copyright (C) 2007 Matti Juvonen <mpj@iki.fi> (RS400 Tools)
Portions Copyright (C) 2006 Jani Hursti <jani.hursti@hut.fi>,
Portions Copyright (C) 2001-2003 Tom Oliveira e Silva

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
************************************************************************/

#include "polarSoftwareOptions.h"

polarSoftwareOptions::polarSoftwareOptions(polar4linuxInternals *internals0)
{
	internals=internals0;
	setupUi(this);
	connect(pushButtonPath, SIGNAL(clicked()), this, SLOT(chooseDataPath()));
	connect(pushButtonImage, SIGNAL(clicked()), this, SLOT(chooseBGImage()));
	connect(pushButtonSave, SIGNAL(clicked()), this, SLOT(saveSettings()));
}

void polarSoftwareOptions::fill()
{
	QString tmp;
	lineEditHRmin->setText(tmp.setNum(internals->default_graph_configuration.FCminDisplay));
	lineEditHRmax->setText(tmp.setNum(internals->default_graph_configuration.FCmaxDisplay));
	lineEditSPEEDmin->setText(tmp.setNum(internals->default_graph_configuration.SPEEDminDisplay));
	lineEditSPEEDmax->setText(tmp.setNum(internals->default_graph_configuration.SPEEDmaxDisplay));
	comboBoxBackground->setCurrentIndex(internals->default_graph_configuration.backgroundType);
	comboBoxBound->setCurrentIndex(internals->default_graph_configuration.boundType);
	lineEditPath->setText(internals->data_path);
	lineEditImage->setText(internals->CalendarBGimages_path);
	comboBoxLanguage->setCurrentIndex(internals->language);
	spinBoxSP0->setValue((int)internals->default_graph_configuration.sportzones[0]);
	spinBoxSP1->setValue((int)internals->default_graph_configuration.sportzones[1]);
	spinBoxSP2->setValue((int)internals->default_graph_configuration.sportzones[2]);
	spinBoxSP3->setValue((int)internals->default_graph_configuration.sportzones[3]);
	spinBoxSP4->setValue((int)internals->default_graph_configuration.sportzones[4]);
	spinBoxSP5->setValue((int)internals->default_graph_configuration.sportzones[5]);
	spinBoxTimeout->setValue((int)internals->connexionTimeout);
	spinBoxHRrest->setValue((int)internals->default_graph_configuration.user_hr_rest);
	spinBoxHRmax->setValue((int)internals->default_graph_configuration.user_hr_max);
	SpinBoxEstimatedSpeed->setValue((float)internals->default_graph_configuration.user_speed);
	lineEdit_ircomm->setText(internals->ircommPath);
	checkBoxDisconnect->setChecked(internals->autodisconnect);
}

void polarSoftwareOptions::chooseBGImage()
{
	QString s = QFileDialog::getOpenFileName(this,
																					 tr("Open Image"),
																					 internals->application_path,
																					 tr("Image Files (*.png *.jpg *.jpeg *.bmp)"));
																					 
	if (s!="") internals->CalendarBGimages_path=s;
	fill();
}

void polarSoftwareOptions::chooseDataPath()
{
	QString s = QFileDialog::getExistingDirectory (this,
																								 tr("Choose hrm directory"),
																								 internals->application_path,
																								 QFileDialog::ShowDirsOnly);
	if (s!="") internals->data_path=s+"/";
	fill();
}



void polarSoftwareOptions::initSettings()
{
	QSettings settings("polar4Linux", "polar4Linux");
	QString defdata_path = internals->application_path+QString("/sample_data/");
	QString defimages_path=internals->application_path+QString("/images/path.jpeg");
	
	settings.beginGroup("Misc");
		internals->autodisconnect=settings.value("autodisconnect",true).toBool();
		internals->ircommPath=settings.value("ircommpath","/dev/ircomm0").toString();
		internals->connexionTimeout=settings.value("connexionTimeout",3).toInt();
		internals->language=settings.value("language", defdata_path).toInt();
		internals->data_path=settings.value("datapath", defdata_path).toString();
		internals->CalendarBGimages_path=settings.value("imagepath", defimages_path).toString();
		internals->default_graph_configuration.backgroundType=settings.value("backgroundType",0).toInt();
		internals->default_graph_configuration.boundType=settings.value("boundType", 0).toInt();
		internals->default_graph_configuration.FCminDisplay=settings.value("displayHRmin", 0).toFloat();
		internals->default_graph_configuration.FCmaxDisplay=settings.value("displayHRmax", 200).toFloat();
		internals->default_graph_configuration.SPEEDminDisplay=settings.value("displaySPEEDmin", 0).toFloat();
		internals->default_graph_configuration.SPEEDmaxDisplay=settings.value("displaySPEEDmax", 20).toFloat();
	settings.endGroup();
	
	settings.beginGroup("Calendar");
	((QWidget*)internals->mainW)->resize(settings.value("size", QSize(450, 500)).toSize());
	((QWidget*)internals->mainW)->move(settings.value("pos", QPoint(20, 60)).toPoint());
	settings.endGroup();

	settings.beginGroup("Plotter");
	((QWidget*)internals->plotW)->resize(settings.value("size", QSize(880, 600)).toSize());
	((QWidget*)internals->plotW)->move(settings.value("pos", QPoint(350, 70)).toPoint());
	settings.endGroup();
	
	settings.beginGroup("Sportzones");
	internals->default_graph_configuration.sportzones[0]=settings.value("zero",50).toInt();
	internals->default_graph_configuration.sportzones[1]=settings.value("one",60).toInt();
	internals->default_graph_configuration.sportzones[2]=settings.value("two",70).toInt();
	internals->default_graph_configuration.sportzones[3]=settings.value("three",80).toInt();
	internals->default_graph_configuration.sportzones[4]=settings.value("four",90).toInt();
	internals->default_graph_configuration.sportzones[5]=settings.value("five",100).toInt();
	internals->default_graph_configuration.user_hr_rest=settings.value("user_hr_rest",50).toInt();
	internals->default_graph_configuration.user_hr_max=settings.value("user_hr_max",200).toInt();
	internals->default_graph_configuration.user_speed=settings.value("user_speed",14).toFloat();
	settings.endGroup();
}


void polarSoftwareOptions::saveSettings()
{
	QSettings settings("polar4Linux", "polar4Linux");
	settings.beginGroup("Misc");
		settings.setValue("connexionTimeout",spinBoxTimeout->value());
		settings.setValue("ircommpath",internals->ircommPath);
		settings.setValue("autodisconnect",internals->autodisconnect);
		settings.setValue("language", comboBoxLanguage->currentIndex());
		settings.setValue("datapath", internals->data_path);
		settings.setValue("imagepath", internals->CalendarBGimages_path);
		
		settings.setValue("backgroundType",comboBoxBackground->currentIndex());
		settings.setValue("boundType", comboBoxBound->currentIndex());
		settings.setValue("displayHRmin", lineEditHRmin->text().toFloat());
		settings.setValue("displayHRmax", lineEditHRmax->text().toFloat());
		settings.setValue("displaySPEEDmin", lineEditSPEEDmin->text().toFloat());
		settings.setValue("displaySPEEDmax", lineEditSPEEDmax->text().toFloat());
	settings.endGroup();
	
	settings.beginGroup("Calendar");
		settings.setValue("size", ((QWidget*)internals->mainW)->size());
		settings.setValue("pos", ((QWidget*)internals->mainW)->pos());
	settings.endGroup();
	
	settings.beginGroup("Plotter");
		settings.setValue("size", ((QWidget*)internals->plotW)->size());
		settings.setValue("pos", ((QWidget*)internals->plotW)->pos());
	settings.endGroup();
	
	settings.beginGroup("Sportzones");
		settings.setValue("zero", spinBoxSP0->value());
		settings.setValue("one", spinBoxSP1->value());
		settings.setValue("two", spinBoxSP2->value());
		settings.setValue("three", spinBoxSP3->value());
		settings.setValue("four", spinBoxSP4->value());
		settings.setValue("five", spinBoxSP5->value());
		settings.setValue("user_hr_rest", spinBoxHRrest->value());
		settings.setValue("user_hr_max", spinBoxHRmax->value());
		settings.setValue("user_speed", SpinBoxEstimatedSpeed->value());
	settings.endGroup();
}

